<?php
/**
 * Lớp đọc/ghi các cấu hình từ cơ sở dữ liệu
 */
class Setting extends Eloquent
{
    public static $timestamps = false;

    public static function get($key)
    {
        $setting = static::where_s_key($key)->first();
        if ($setting) {
            return $setting->value;
        };

        return null;
    }

    public static function set($s_key, $value, $description = null)
    {
        $setting = static::where_s_key($s_key)->first();
        if ($setting && $setting->editable) {
            $setting->value = $value;
            if (is_not_null($description)) {
                $setting->description = $description;
            }
            $setting->save();
        } else {
            $setting = static::insert(compact('s_key', 'value', 'description'));
        }

        return $setting;
    }

    public static function all_settings()
    {
        return static::where_editable(true)->get();
    }
}